package controller;

import databaseLayer.StatisticsLookUp;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import movieBean.Movie;
import statistics.*;
import userBean.UserIdentity;

/**
 *
 * @author Sam
 */
public class Statistics extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String address = "/viewStat.jsp";
        String viewMode;
        
        HttpSession session = request.getSession();
        if (session.isNew() || !(((UserIdentity)session.getAttribute("userIdentity")).getUserType().equals("TicketingManager"))) {
            if (session.isNew()) {
                session.invalidate();
            }
            address = "/index.do";
            response.sendRedirect(address);
        }
        
        viewMode = request.getParameter("viewMode");
        if (viewMode == null || viewMode == "") {
            viewMode = "movie";
        }
        
        if (viewMode.equals("movieSlot")) {
            int movieID = Integer.parseInt(request.getParameter("movieID"));
            int slotNo = Integer.parseInt(request.getParameter("slotNo"));
            
            request.setAttribute("movieID", movieID);
            request.setAttribute("slotNo", slotNo);
            
            ServletContext application = getServletContext();
            String slotID = ((Movie[])application.getAttribute("allMoviesDetail"))[movieID].getMovieSlot()[slotNo].getSlotID();
            
            MovieSlotStatistics movieSlotStatistics = StatisticsLookUp.getMovieSlotStatistics(slotID);
            request.setAttribute("movieSlotStatistics", movieSlotStatistics);
        }
        else if (viewMode.equals("house")) {
            HouseStatistics[] houseStatistics = StatisticsLookUp.getHouseStatistics();
            request.setAttribute("houseStatistics", houseStatistics);
        }
        else {
            MovieStatistics[] movieStatistics = StatisticsLookUp.getMovieStatistics();
            request.setAttribute("movieStatistics", movieStatistics);
        }
        
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
